layui.use(['form', 'layer', 'table', 'laytpl'], function() {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : top.layer,
		$ = layui.jquery,
		laytpl = layui.laytpl,
		table = layui.table;
	//任务列表
	var tableIns = table.render({
		elem: '#taskList',
		url: '/myremind/job/list',
		method: 'post',
		contentType: 'application/json',
		cellMinWidth: 95,
		page: {
			layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
			groups: 5,
			first: '首页',
			last: '尾页',
			limits: [10, 15, 20, 25],
			limit: 10
		},
		height: 'full-125',
		id: 'taskListTable',
		cols: [
			[{
					type: 'checkbox',
					fixed: 'left',
					width: 30
				},
				{
					title: '序号',
					type: 'numbers',
					width: 40
				},
				{
					field: 'jobName',
					title: '任务名称',
					align: 'center'
				},
				{
					field: 'jobGroup',
					title: '任务分组',
					align: 'center'
				},
				{
					field: 'description',
					title: '描述',
					align: 'center'
				},
				{
					field: 'jobClassName',
					title: '执行类',
					align: 'center'
				},
				{
					field: 'cronExpression',
					title: 'cron',
					align: 'center'
				},
				{
					title: '操作',
					minWidth: 150,
					templet: '#taskListBar',
					fixed: "right",
					align: "center"
				}
			]
		]
	});

	//搜索【此功能需要后台配合，所以暂时没有动态效果演示】
	$(".search_btn").on("click", function() {
		if ($(".searchVal").val() != '') {
			table.reload("taskListTable", {
				page: {
					curr: 1 //重新从第 1 页开始
				},
				where: {
					key: $(".searchVal").val() //搜索的关键字
				}
			})
		} else {
			layer.msg("请输入搜索的内容");
		}
	});

	// 启动任务
	function trigger(info) {
		layer.confirm('确定启动此任务？', {
			icon: 3,
			title: '提示信息'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/myremind/job/resume",
				data: JSON.stringify(info),
				contentType: 'application/json;charset=UTF-8',
				success: function(result) {
					tableIns.reload();
					layer.close(index);
				}
			});
		});
	}

	//添加任务
	function addBook() {
		var index = layui.layer.open({
			title: "添加任务",
			type: 2,
			content: "taskAdd.html",
		})
		layui.layer.full(index);
		window.sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
		$(window).on("resize", function() {
			layui.layer.full(window.sessionStorage.getItem("index"));
		})
	}
	//修改任务
	function editBook(edit) {
		var index = layui.layer.open({
			title: "修改书籍",
			type: 2,
			content: "taskEdit.html",
			success: function(layer, index) {
				var body = layui.layer.getChildFrame('body', index);
				if (edit) {
					body.find(".uuid").val(edit.uuid); //id
					form.render();
				}
				setTimeout(function() {
					layui.layer.tips('点击此处返回书籍列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				}, 500)
			}
		})
		layui.layer.full(index);
		window.sessionStorage.setItem("index", index);
		//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
		$(window).on("resize", function() {
			layui.layer.full(window.sessionStorage.getItem("index"));
		})
	}
	// 恢复任务
	function resumeTask(info) {
		layer.confirm('确定恢复此任务？', {
			icon: 3,
			title: '提示信息'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/myremind/job/resume",
				data: JSON.stringify(info),
				contentType: 'application/json;charset=UTF-8',
				success: function(result) {
					tableIns.reload();
					layer.close(index);
				}
			});
		});
	}
	// 停止任务
	function stopTask(info) {
		layer.confirm('确定停止此任务？', {
			icon: 3,
			title: '提示信息'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/myremind/job/pause",
				data: JSON.stringify(info),
				contentType: 'application/json;charset=UTF-8',
				success: function(result) {
					tableIns.reload();
					layer.close(index);
				}
			});
		});
	}
	// 移除任务
	function delTask(info) {
		layer.confirm('确定移除此任务？', {
			icon: 3,
			title: '提示信息'
		}, function(index) {
			$.ajax({
				type: "post",
				url: "/myremind/job/remove",
				data: JSON.stringify(info),
				contentType: 'application/json;charset=UTF-8',
				success: function(result) {
					tableIns.reload();
					layer.close(index);
				}
			});
		});
	}
	// 增加任务
	$(".addNews_btn").click(function() {
		addBook();
	})

	//批量移除
	$(".delAll_btn").click(function() {
		var checkStatus = table.checkStatus('taskListTable'),
			data = checkStatus.data,
			newsId = [];
		if (data.length > 0) {
			for (var i in data) {
				newsId.push(data[i].newsId);
			}
			layer.confirm('确定移除选中的任务？', {
				icon: 3,
				title: '提示信息'
			}, function(index) {
				// $.get("删除文章接口",{
				//     newsId : newsId  //将需要删除的newsId作为参数传入
				// },function(data){
				tableIns.reload();
				layer.close(index);
				// })
			})
		} else {
			layer.msg("请选择需要移除的任务");
		}
	})

	//列表操作
	table.on('tool(taskList)', function(obj) {
		var layEvent = obj.event,
			data = obj.data;

		if (layEvent === 'trigger') { //执行
			viewBook(data);
		} else if (layEvent === 'edit') { //编辑
			editBook(data);
		} else if (layEvent === 'del') { //删除
			delTask(data);
		} else if (layEvent === 'stop') { //删除
			stopTask(data);
		}
	});

})
